package com.example.utils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    public final static String IMAGE_SUFFIX = "CQ:image,file=";



    public static boolean isEmpty(String str){
        if(str == null){
            return true;
        }
        if(str.trim().length() <=0){
            return true;
        }
        return false;
    }

    /**
     * @param str
     * @param flag 是否统计图片的保存字符串长度
     * @return
     */
    public static int getStrLength(String str,boolean flag){
        if(isEmpty(str)){
            return 0;
        }
        if(!flag){
            str = replaceAllCenterBrackets(str);
        }
        return str.length();
    }

    /**替换所有中括号
     * @param str
     * @return
     */
    public static String replaceAllCenterBrackets(String str){
        if(isEmpty(str))return str;
        return str.replaceAll("\\[(.*?)\\]", "");
    }

    /**替换所有括号
     * @param str
     * @return
     */
    public static String replaceAllBrackets(String str){
        if(StringUtils.isNotEmpty(str)){
            str = str.replaceAll("\\((.*?)\\)", "");
        }
        return str;
    }

    /**替换所有特殊字符
     * @param str
     * @return
     */
    public static String replaceSpecialChar(String str){
        if(StringUtils.isNotEmpty(str)){
            str = str.replaceAll("#!ALB\\(37\\)","").replaceAll("#n","").replaceAll("#!ALB","");
        }
        return str;
    }

    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }

    public static boolean isNum(char c){
        return  java.lang.Character.isDigit(c);
    }

    public static boolean isNumString(String str){
        boolean empty = isEmpty(str);
        if(empty){
            return false;
        }
        boolean next = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if(!isNum(c)){
                next = false;
                break;
            }
        }
        return next;
    }

    public static String getThreeLen(int id){
        if(id<10){
            return "00"+id;
        }
        if(id<100){
            return "0"+id;
        }
        return id+"";
    }

    /**获取消息中的指定数量图片
     * @param msg 消息
     * @param num 获取前几个图片 -1代表全部
     * @return
     */
    public static List<String> getMsgImages(String msg, int num){
        List<String> images = new ArrayList<String>();
        String regex = "\\[(.*?)]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(msg);
        int temp = 0;
        while (matcher.find()) {
            String content = matcher.group(1);
            if(content.startsWith(IMAGE_SUFFIX)) {
                images.add(content.replace(IMAGE_SUFFIX, "").trim());
                if(num!=-1&&(temp+=1)==num){
                    break;
                }
            }
        }
        return images;
    }

    /**获取消息中第一个图片
     * @param msg  消息
     * @return
     */
    public static String getMsgFirstImage(String msg){
        List<String> msgImages = getMsgImages(msg, 1);
        if(msgImages.size()>0){
            return msgImages.get(0);
        }
        return null;
    }

    /**修改消息文本中图片地址
     *
     * @param msg
     * @return
     */
    public static String replaceMsgImageAddress(String msg,String prefix){
        String regx = "\\[CQ:image,file=(.*?)]";
        return msg.replaceAll(regx,"[CQ:image,file="+prefix+"$1]");
    }


    /**获取该字符串中所有的 表情 图片 emji等
     * @param str
     * @return
     */
    public static Map<String,List> getAll(String str){
        List<Integer> faces = new ArrayList<Integer>();
        List<Integer> emojis = new ArrayList<Integer>();
        List<String> images = new ArrayList<String>();
        List<Long> ats = new ArrayList<Long>();

        String regex = "\\[(.*?)]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            String content = matcher.group(1);
            if(content.startsWith("CQ:at,qq=")){
               String qq = content.replace("CQ:at,qq=","").trim();
               if(StringUtils.isNumString(qq)){
                   ats.add(Long.parseLong(qq));
               }
            }else if(content.startsWith(IMAGE_SUFFIX)){
               String file = content.replace(IMAGE_SUFFIX,"").trim();
               images.add(file);
            }else if(content.startsWith("CQ:face,id=")){
                String id = content.replace("CQ:face,id=","").trim();
                if(StringUtils.isNumString(id)){
                    faces.add(Integer.parseInt(id));
                }
            }else if(content.startsWith("CQ:emoji,id=")){
                String id = content.replace("CQ:emoji,id=","").trim();
                if(StringUtils.isNumString(id)){
                    emojis.add(Integer.parseInt(id));
                }
            }

        }
        Map<String,List> map = new HashMap<String, List>();
        map.put("faces",faces);
        map.put("emojis",emojis);
        map.put("images",images);
        map.put("ats",ats);
        return map;
    }

    /**获取满级技能描述 去除括号
     * @param str
     * @return
     */
    public static String getFullDesc(String str){
        List<String> behindNums = new ArrayList<String>();
        final String pattern2 = "\\((.*?)\\)";
        Pattern p= Pattern.compile(pattern2);
        Matcher m=p.matcher(str);
        while(m.find()){
            String temp ="「"+(m.group().replaceAll("\\(","").replaceAll("\\)",""))+"」";
            behindNums.add(temp);
        }
        str = str.replaceAll(pattern2,"B");

        StringBuilder stringBuffer = new StringBuilder();
        int temp = -1;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if(StringUtils.isNum(c)||c=='B'||c=='.'){
                StringBuilder tempNum = new StringBuilder();
                boolean behindIsBrackets = false;
                for (int j = i; j < str.length(); j++) {
                    char x = str.charAt(j);
                    if(x=='.'||StringUtils.isNum(x)){
                        tempNum.append(x);
                    }else{
                        if(x=='B'){
                            i=j;
                            behindIsBrackets = true;
                            break;
                        }else{
                            i=j-1;
                            break;
                        }
                    }
                }
                if(behindIsBrackets){
                    temp+=1;
                    stringBuffer.append("{").append(temp).append("}");
                }else{
                    stringBuffer.append(tempNum);
                }
            }else{
                stringBuffer.append(c);
            }
        }

        return MessageFormat.format(stringBuffer.toString(),behindNums.toArray());
    }

}
